﻿<?php
class UserAction extends CommonAction{
	
	//用户列表
	public function index(){
		$this->title = "用户管理";
		$keyword = I("keyword",'','trim');
		$this->keyword = $keyword;
		$where = array();
		if($keyword){
			$where['phone'] = array('like',"%{$keyword}%");
		}
		$User = D("user");
		import('ORG.Util.Page');
		$count = $User->where($where)->count();
		$Page  = new Page($count,25);
		$Page->setConfig('theme','共%totalRow%条记录 | 第 %nowPage% / %totalPage% 页 %upPage%  %linkPage%  %downPage%');
		$show  = $Page->show();
		$list = $User->where($where)->order('addtime Desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $key=>$val){
			$minfo=M("userinfo")->where("user='{$val['phone']}'")->find();
			$list[$key]['bankname']=$minfo['bankname'];
			$list[$key]['bankcard']=$minfo['bankcard'];
			$list[$key]['is_sheng']=$minfo['is_sheng'];
			$list[$key]['user_type']=$minfo['user_type'];
			$list[$key]['tuijianren']=M("yaoqingma")->where("code='{$val['yaoqingma']}'")->getField("name");
		}
		//dump($list);exit;
		$this->list = $list;
		$this->page = $show;
		$this->display();
	}
	
	//允许/禁止用户登录
	public function status(){
		$this->title = "更改用户状态";
		$id = I("id",0,'trim');
		if(!$id){
			$this->error("参数错误!");
		}
		$User = D("user");
		$info = $User->where(array('id' => $id))->find();
		if(!$info){
			$this->error("用户不存在!");
		}
		$newstatus = empty($info['status'])?1:0;
		$status = $User->where(array('id' => $id))->save(array('status' => $newstatus));
		if(!$status){
			$this->error("操作失败!");
		}
		$this->success("变更用户状态成功!");
	}
	
	//删除用户
	public function del(){
        $this->title='删除用户';
        $id = I('id',0,'trim');
        if(!$id){
            $this->error("参数有误!");
        }
        $User = D("user");
        $status = $User->where(array('id' => $id))->delete();
        if(!$status){
            $this->error("删除失败!");
        }
        $this->success("删除用户成功!");
	}
	
	
	//修改用户密码
	public function changepass(){
		$data = array('status' => 0,'msg' => '未知错误');
        $id = I('post.id',0,'trim');
		$pass = I("post.pass",'','trim');
        if(!$id || !$pass){
            $data['msg'] = "参数有误!";
        }else{
        	$User = D("user");
			$pass = sha1(md5($pass));
			$status = $User->where(array('id' => $id))->save(array('password' => $pass));
			if(!$status){
				$data['msg'] = "操作失败!";
			}else{
				$data['status'] = 1;
			}
        }
		$this->ajaxReturn($data);
	}
	
	//查看用户资料
	public function userinfo(){
		$this->title = "查看用户资料";
		$user = I("user",'','trim');
		if(!$user){
			$this->error("参数错误!");
		}
		$Userinfo = D("userinfo");
		$info = $Userinfo->where(array('user' => $user))->find();
		$invitation = D('User')->field('invitation')->where(['phone'=>$user])->find();
		$this->invitation = $invitation['invitation'];
		$this->baseinfo = $info;

		/*
		$Otherinfo = D("Otherinfo");
		$info = $Otherinfo->where(array('user' => $user))->find();
		$info = json_decode($info['infojson']);
		$this->otherinfo = $info;
		*/
	
		$shanginfo = D("shanginfo");
		$info = $shanginfo->where(array('user' => $user))->find();
		$info = json_decode($info['infojson']);
		$this->shanginfo = $info;


		$qiyeinfo = D("qiyeinfo");
		$info = $qiyeinfo->where(array('user' => $user))->find();
		$info = json_decode($info['infojson']);
		$this->qiyeinfo= $info;

		// 芝麻信用分
		//import('Org.Util.Zhima');
		
		//$zm = new \TestZhimaAuthInfoAuthorize();

		// $data = $zm->getScore($this->baseinfo['zhima_openid']);
		
		if($data['zm_score']){
			$this->assign('score',$data['zm_score']);
		}
		$updata = M('userphone')->where(['user'=>$user])->select();
		$this->assign('updata',$updata);
		$this->display();
	}
	

	public function getMobileInfo(){
		if(IS_AJAX){
			$post = I('post.','');
			


			header('Content-type:text/html;charset=utf-8');
	
			//配置您申请的appkey和apiSecret
			$apiUrl = "https://t.limuzhengxin.cn:3443/api/gateway";
			$appkey = C('cfg_limu_apikey');//!!!需自行设定!!!
			$apiSecret = C('cfg_limu_apisecret');//!!!需自行设定!!!
			$version = "1.2.0";
			$callBackUrl = "";//回调函数非必填
			$identityCardNo = "";//查询用户的身份证号非必填
			$identityName = "";//查询用户的真实姓名非必填
			//运营商请求参数
			$bizType = "mobile"; //采集类型
			$method = "api.mobile.get";//接口名称

			//业务参数783053 13570407802
			$username = $post['phone'];//用户名 !!!需自行设定!!!
			$password = base64_encode("".$post['pass']."");//用户密码 //!!!需自行设定!!!
			$contentType = "sms";//查询指定内容 //!!!需自行设定!!!
			$otherInfo = "";// !!!需自行设定!!!

			//获取结果时间次数设定
			$count = 50; //尝试次数
			$sleepCount = 10;//间隔时间

			$token = "";//请求数据流程唯一标志


			$res = mobile($apiUrl,$appkey,$apiSecret,$version,$callBackUrl,$identityCardNo,$identityName,$bizType,$method,$username,$password,$contentType,$otherInfo,$count,$sleepCount,$token);

			// $json = '{"code":"0000","msg":"成功","data":{"callRecordInfo":[{"callAddress":"广州","callDateTime":"2017-06-07 11:22:45","callTimeLength":"87","callType":"被叫","mobileNo":"02028093638"},{"callAddress":"广州","callDateTime":"2017-06-06 14:57:13","callTimeLength":"29","callType":"被叫","mobileNo":"02028059406"},{"mobileNo":"13570407802","sendSmsToTelCode":"1065849","sendSmsAddress":"","sendSmsTime":"2017-01-01 00:00:00","sendType":"接收"}],"netInfo":[],"businessInfo":[]}}';
		
			// $arr = json_decode($json,true)['data']['callRecordInfo'];
			// $json = json_encode($arr);
			
			
			// $this->ajaxReturn(['errdata'=>$json]);

			if($res['errcode'] == 0){
				
				
				$this->ajaxReturn(['errcode'=>0,'errmsg'=>$res['errmsg']]);

			}


			if($res['errcode'] == 1){
				


				$result = json_decode($res['data'],true)['data']['callRecordInfo'];
				$udata = M('userphone')->where(['user'=>$post['phone']])->find();
				if(is_null($udata)){
					
					foreach($result as $v){
	
						M('userphone')->add([
								'user'=>$post['phone'],
								'phone_num' => $v['mobileNo'],
								'phone_address' => $v['callAddress'],
								'phone_time' => $v['callDateTime'],
								'phone_calltime' => $v['callTimeLength'],
								'phone_type' => $v['callType'],

							]);
					}
				}else{
					M('userphone')->where(['user'=>$post['phone']])->delete();
					foreach($result as $rv){
	
						M('userphone')->add([
								'user'=>$post['phone'],
								'phone_num' => $rv['mobileNo'],
								'phone_address' => $rv['callAddress'],
								'phone_time' => $rv['callDateTime'],
								'phone_calltime' => $rv['callTimeLength'],
								'phone_type' => $rv['callType'],

							]);
					}
				}
				



				$this->ajaxReturn(['errdata'=>$result]);
			}

	

			
		}
		
	}
	
	/**
	 * 修改银行卡
	 */
	public function edit_bank(){
		$phone=$_GET['phone'];
		$userinfo=M("userinfo")->where("user='{$phone}'")->find();
		//dump($userinfo);
		$this->assign('userinfo',$userinfo);
		$this->display();
	}
	
	public function act_save_bank(){
		$bankname=$_POST['bankname'];
		$bankcard=$_POST['bankcard'];
		$id=$_POST['id'];
		//dump($_POST);
		$data['bankname']=$bankname;
		$data['bankcard']=$bankcard;
		$ret=M("userinfo")->where("id=$id")->save($data);
		
		if($ret){
			exit("1");
		}else{
			exit("0");
		}
	}
	
	public function sheng_edit(){
		$id=$_GET['id'];
		$user_phone=M("user")->where("id=$id")->getField("phone");
		$map['user']=$user_phone;
		$userinfo=M("userinfo")->where($map)->find();


		/*新增*/
		$user_user=M("user")->where("id=$id")->getField("phone");
		$serinfo= M("yinhangmianqian")->where(array('user' => $user_user))->find();
		/*新增*/


		$this->assign('userinfo',$userinfo);
		$this->assign('serinfo',$serinfo);

		$this->display();
	}
	
	public function act_save_is_sheng(){
		$id=$_POST['id'];
		$is_sheng=$_POST['is_sheng'];
		
		
		$map['id']=$id;
		$data['is_sheng']=$is_sheng;
		$data['sheng_tongguo_rate']=$_POST['sheng_tongguo_rate'];
		$data['sheng_erdu']=$_POST['sheng_erdu'];
		$data['sheng_time']=time();
		//dump($data);
		$ret=M("userinfo")->where($map)->save($data);
		
		if($ret===false){
			$this->error("保存失败！");
		}else{
			$this->success("保存成功！");
		}
		$this->ajaxReturn($data);
	}
	
	public function act_save_is_sheng1(){
		$id=$_POST['id'];
		$status=$_POST['status'];
		$user_phone=M("user")->where("id=$id")->getField("phone");
		
		$map['user']=$user_phone;
		$data['is_sheng']=$status;
		
		$ret=M("userinfo")->where($map)->save($data);
		
		if(!$ret){
			$data['msg'] = "操作失败!";
		}else{
			$data['status'] = 1;
		}
		$this->ajaxReturn($data);
	}
	
	/**
	 * 银行审核
	 */
	public function bank_sheng(){
		$user=$_GET['user'];
		$banks=M("banks")->select();
		foreach($banks as $key=>$val){
			$map['user']=$user;
			$map['bank_id']=$val['id'];
			
			$banks[$key]['bank_sheng']=M("bank_sheng")->where($map)->find();
		}
		//dump($banks);
		$this->assign('user',$user);
		$this->assign('banks',$banks);
		$this->display();
	}
	
	public function bank_sheng_edit(){
		$bank_id=$_GET['bank_id'];
		$user=$_GET['user'];

		$map['user']=$user;
		$map['bank_id']=$bank_id;		
		$bank_sheng=M("bank_sheng")->where($map)->find();
		$bank_sheng['status']=unserialize($bank_sheng['status']);
		$bank=M("banks")->where("id={$bank_id}")->find();


		/*新增*/
		$serinfo= M("yinhangmianqian")->where(array('user' => $user,'yinhang'=>$bank_id))->find();
		/*新增*/



		$this->assign("bank_sheng",$bank_sheng);
		$this->assign("bank",$bank);
		$this->assign("bank_id",$bank_id);
		$this->assign("user",$user);
		/*新增*/
		$this->assign("serinfo",$serinfo);
		/*新增*/
		$this->display();
	}
	
	public function act_save_bank_sheng(){
		$bank_id=$_POST['bank_id'];
		$user=$_POST['user'];
		$status=$_POST['status'];
		$sheng_type=$_POST['sheng_type'];
		$status=$_POST['sheng'];
		$content=$_POST['content'];
		
		$map['bank_id']=$bank_id;
		$map['user']=$user;
		
		$map['bank_id']=$bank_id;
		$map['user']=$user;
		$bank_sheng=M("bank_sheng")->where($map)->find();
		
		$data['bank_id']=$bank_id;
		$data['user']=$user;
		$data['status']=serialize($status);
		$data['sheng_type']=$sheng_type;
		$data['content']=$content;
		if(empty($bank_sheng)){
			$ret=M("bank_sheng")->add($data);
		}else{
			$ret=M("bank_sheng")->where("id={$bank_sheng['id']}")->save($data);
		}
		
	
		if($ret===false){
			$this->error("保存失败！");
		}else{
			$this->success("保存成功！");
		}
		$this->ajaxReturn($data);
	}
	
	public function news(){
		$news = D("user_news n");
		$uid=$_GET['uid'];
		$where['n.uid']=$uid;
		import('ORG.Util.Page');
		
		$field="n.*,u.phone";
		$count = M("user_news n")->join("user as u on n.uid=u.id")->field($field)->where($where)->count();
		$Page  = new Page($count,25);
		$Page->setConfig('theme','共%totalRow%条记录 | 第 %nowPage% / %totalPage% 页 %upPage%  %linkPage%  %downPage%');
		$show  = $Page->show();
		$list = M("user_news n")->join("user as u on n.uid=u.id")->field($field)->where($where)->order('n.addtime Desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $key=>$val){
			$list[$key]['name']=M("userinfo")->where("user='{$val['phone']}'")->getField("name");
		}
		//dump($list);exit;
		$this->assign('uid',$uid);
		$this->list = $list;
		$this->page = $show;
		$this->display();
	}
	
	public function news_edit(){
		$id=intval($_GET['id']);
		$uid=$_GET['uid'];
		if($id>0){
			$detail=M("user_news")->where("id=$id")->find();
			//dump($detail);exit;
			$uid=$detail['uid'];
			$this->assign('data',$detail);
		}
		
		$this->assign('uid',$uid);
		$this->display();
	}
	
	public function save_user_news(){
		$id=intval($_POST['id']);
		$uid=$_POST['uid'];
		$post=$_POST['post'];
		
		if($id>0){
			//dump($post);exit;
			
			$ret=M("user_news")->where("id=$id")->save($post);
		}else{
			$post['uid']=$uid;
			$post['addtime']=time();
			$ret=M("user_news")->add($post);
		}
		if($ret){
			$this->success('保存成功！');
		}else{
			$this->error('保存失败！');
		}
	}
	
	public function user_news_del(){
		$id=$_GET['id'];
		$ret=M("user_news")->where("id=$id")->delete();
		if($ret){
			$this->success('保存成功！');
		}else{
			$this->error('保存失败！');
		}
	}
	
	public function yaoqingma(){
		
		import('ORG.Util.Page');		
		$count = M("yaoqingma")->where($where)->count();
		$Page  = new Page($count,25);
		$Page->setConfig('theme','共%totalRow%条记录 | 第 %nowPage% / %totalPage% 页 %upPage%  %linkPage%  %downPage%');
		$show  = $Page->show();
		$list = M("yaoqingma")->field($field)->where($where)->order('id Desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $key=>$val){
			//$list[$key]['name']=M("userinfo")->where("user='{$val['phone']}'")->getField("name");
		}
		//dump($list);exit;
		
		$this->list = $list;
		$this->page = $show;
		$this->display();
	}
	
	public function act_save_yaoqingma(){
		$name=$_POST['name'];
		$code=rand(100000,999999);
		
		$data['name']=$name;
		$data['code']=$code;
		$data['addtime']=time();
		$ret=M("yaoqingma")->add($data);
		if($ret){
			$this->success('保存成功！');
		}else{
			$this->error('保存失败！');
		}
	}
	
	public function del_yaoqingma(){
		$id=$_GET['id'];
		$ret=M("yaoqingma")->where("id=$id")->delete();
		if($ret){
			$this->success('删除成功！');
		}else{
			$this->error('删除失败！');
		}
	}






	/*编辑商证*/
	public function shangzhen_edit(){
		$user=$_GET['user'];
		$map['user']=$user;	
		$bank_sheng=M("shangzhen_sheng")->where($map)->find();
		$bank_sheng['status']=unserialize($bank_sheng['status']);


		$shanginfo = D("shanginfo");
		$info = $shanginfo->where(array('user' => $user))->find();
		$info = json_decode($info['infojson']);
		$this->shanginfo = $info;

		$this->assign("bank_sheng",$bank_sheng);
		$this->assign("user",$user);
		$this->display();
	}
	/*编辑商证*/




	/*编辑流水*/
	public function gongsiliushui_edit(){
		$user=$_GET['user'];
		$map['user']=$user;	
		$bank_sheng=M("liushui_sheng")->where($map)->find();
		$bank_sheng['status']=unserialize($bank_sheng['status']);

		$qiyeinfo = D("qiyeinfo");
		$info = $qiyeinfo->where(array('user' => $user))->find();
		$info = json_decode($info['infojson']);
		$this->qiyeinfo= $info;

		
		$this->assign("bank_sheng",$bank_sheng);
		$this->assign("user",$user);
		$this->display();
	}

	/*编辑流水*/


	/*保存商证*/
	public function shang_save_is_sheng(){

		$user=$_POST['user'];

		$status=$_POST['status'];

		$status=$_POST['sheng'];

		$content=$_POST['content'];
		
		
		$map['user']=$user;


		$bank_sheng=M("shangzhen_sheng")->where($map)->find();
		

		$data['user']=$user;
		$data['status']=serialize($status);

		$data['content']=$content;
		if(empty($bank_sheng)){
			$ret=M("shangzhen_sheng")->add($data);
		}else{
			$ret=M("shangzhen_sheng")->where("id={$bank_sheng['id']}")->save($data);
		}
		
	
		if($ret===false){
			$this->error("保存失败！");
		}else{
			$this->success("保存成功！");
		}
		$this->ajaxReturn($data);
	}



	/*保存流水*/	
	public function liu_save_is_sheng(){

		$user=$_POST['user'];
		
		$status=$_POST['status'];

		$status=$_POST['sheng'];

		$content=$_POST['content'];
		
		$map['user']=$user;
		$bank_sheng=M("liushui_sheng")->where($map)->find();

		$data['user']=$user;
		$data['status']=serialize($status);

		$data['content']=$content;
		if(empty($bank_sheng)){
			$ret=M("liushui_sheng")->add($data);
		}else{
			$ret=M("liushui_sheng")->where("id={$bank_sheng['id']}")->save($data);
		}
		
		if($ret===false){
			$this->error("保存失败！");
		}else{
			$this->success("保存成功！");
		}
		$this->ajaxReturn($data);

	}




}
